package development.hdk.mindmapfornandroid;

import java.util.LinkedList;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import development.hdk.database.BackEnd;
import development.hdk.node.Node;
import development.hdk.node.*;


public class ProcessorActivity extends Activity {
	private BackEnd backEnd;
	private CommonResources commonResources;
	//private DialogHandler dialogHandler;
	private Node focus;
	public boolean fromUI = false;
	public RelativeLayout relativeLayout;
	private Node root;
	private int surfaceColor;
	private int treeID;
	private UserInterface ui;

@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	this.backEnd = new BackEnd(this);
	this.commonResources = new CommonResources(getWindowManager().getDefaultDisplay(), this);
	//this.dialogHandler = new DialogHandler(this, this.commonResources);
	this.ui = new UserInterface(this, this.commonResources);
	this.relativeLayout = new RelativeLayout(this);
    this.relativeLayout.setOnTouchListener(this.ui);
    setContentView(this.relativeLayout);
    Bundle localBundle = getIntent().getExtras();
    this.treeID = localBundle.getInt("id");
    this.surfaceColor = localBundle.getInt("surfaceColor");
    this.commonResources.surfaceColor = this.surfaceColor;
    this.relativeLayout.setBackgroundColor(this.surfaceColor);
    this.backEnd.open();
    //getNodes();
    this.ui.setRoot(this.root);
    this.commonResources.goToStartPosition();
    this.root.updateAllLayout();
}
private void setFocus(Node root2) {
	// TODO Auto-generated method stub
	
}
public void showCustomDialog(int paramInt)
{
  showDialog(paramInt);
}
public void panelDeleteNode()
{
  deleteNode(this.focus);
}
public void deleteNode(Node paramNode)
{
  if (paramNode == this.root);
  while (true)
  {
    
    if (this.focus == paramNode)
      setFocus(paramNode.getParentNode());
    this.relativeLayout.removeView(paramNode);
    this.relativeLayout.removeView(paramNode.line);
    this.backEnd.deleteNode(paramNode);
    paramNode.getParentNode().removeChild(paramNode);
    LinkedList localLinkedList = paramNode.getChildren();
    for (int i = 0; i < localLinkedList.size(); i++)
    deleteNode((Node)localLinkedList.get(i));
    return;
  }
}
public void setStyle(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
{
  dismissDialog(7);
  setSurfaceColor(paramInt4, false);
  changeAllNodes(this.root, paramInt1, paramInt2, paramInt3);
  this.backEnd.setStyle(this.treeID, paramInt1, paramInt2, paramInt3);
  this.root.updateAllLayout();
}
public void setSurfaceColor(int paramInt, boolean paramBoolean)
{
  if (paramBoolean)
    dismissDialog(6);
  this.surfaceColor = paramInt;
  this.commonResources.surfaceColor = paramInt;
  this.relativeLayout.setBackgroundColor(paramInt);
  this.backEnd.updateSurfaceColor(this.treeID, paramInt);
  this.relativeLayout.invalidate();
}
private void changeAllNodes(Node paramNode, int paramInt1, int paramInt2, int paramInt3)
{
  Node localNode = setNodeShape(paramInt1, paramNode);
  localNode.setStyle(paramInt2, paramInt3);
  LinkedList localLinkedList = (LinkedList)localNode.getChildren().clone();
  for (int i = 0; ; i++)
  {
    if (i >= localLinkedList.size())
      return;
    changeAllNodes((Node)localLinkedList.get(i), paramInt1, paramInt2, paramInt3);
  }
}
public void setNodeShape(int paramInt)
{
  dismissDialog(2);
  Node localNode = setNodeShape(paramInt, this.focus);
  this.backEnd.updateNodeShape(localNode);
  this.root.updateAllLayout();
}
public Node setNodeShape(int paramInt, Node paramNode)
{
  Node localNode = paramNode.getParentNode();
  if (localNode != null)
    localNode.removeChild(paramNode);
  this.relativeLayout.removeView(paramNode);
  this.relativeLayout.removeView(paramNode.line);
  Object localObject = new Node(this);
  switch (paramInt)
  {
  default:
    if (localNode == null)
      break;
    localNode.addChild((Node)localObject);
    ((Node)localObject).line = new Line(this, (Node)localObject, this.commonResources);
    RelativeLayout.LayoutParams localLayoutParams2 = ((Node)localObject).line.getRelativeLayoutParams();
    this.relativeLayout.addView(((Node)localObject).line, localLayoutParams2);
    localNode.bringToFront();
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
  }
  while (true)
  {
    if (this.focus == paramNode)
      setFocus((Node)localObject);
    ((Node)localObject).setOnTouchListener(this.ui);
    Rect localRect = ((Node)localObject).getNodeBounds();
    RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.LayoutParams(localRect.right, localRect.bottom);
    localLayoutParams1.leftMargin = this.commonResources.pixelCoordinateX(((Node)localObject).getLocationX());
    localLayoutParams1.topMargin = this.commonResources.pixelCoordinateY(((Node)localObject).getLocationY());
    this.relativeLayout.addView((View)localObject, localLayoutParams1);
    ((Node)localObject).attributes.setShape(paramInt);
    /*this.adView.bringToFront();
    return localObject;
    localObject = new NoBorderNode(this, paramNode);
    break;
    localObject = new RectangleNode(this, paramNode);
    break;
    localObject = new RoundedRectangleNode(this, paramNode);
    break;
    localObject = new OvalNode(this, paramNode);
    break;
    localObject = new CircularNode(this, paramNode);
    break;*/
    this.root = ((Node)localObject);
    this.ui.setRoot(this.root);
  }
}
public void addNode(int paramInt1, int paramInt2, String paramString)
{
  Node localNode = createNode(paramInt1, paramInt2);
  int[] arrayOfInt = new int[2];
  this.relativeLayout.getLocationInWindow(arrayOfInt);
  this.commonResources.setTopLeftX(this.commonResources.floatCoordinateX(paramInt1) + arrayOfInt[0] - this.commonResources.floatLength(Math.round(this.commonResources.getDisplayWidth() / 2.0F)));
  this.commonResources.setTopLeftY(this.commonResources.floatCoordinateY(paramInt2) + arrayOfInt[1] - this.commonResources.floatLength(Math.round(this.commonResources.getDisplayHeight() / 2.0F)));
  setFocus(localNode);
  if (paramString.length() < 100)
    this.focus.setText(paramString);
  while (true)
  {
    this.backEnd.updateNodeText(this.focus);
    this.root.updateAllLayout();
    this.focus.setText(paramString.substring(0, 100));
    return;
  }
}

public void addNode(String paramString)
{
  if (this.fromUI)
    addNode(this.ui.newX, this.ui.newY, paramString);
  while (true)
  {

    addNode(this.focus.getNodeX() + Math.round(this.focus.getNodeWidth() / 2.0F), this.focus.getNodeY() + 2 * this.focus.getNodeHeight(), paramString);
    return;
  }
}

public Node createNode(int paramInt1, int paramInt2)
{
  Attributes localAttributes = new Attributes(this.focus.attributes);
  int i = this.backEnd.createNode(this.treeID, this.commonResources.floatCoordinateX(paramInt1), this.commonResources.floatCoordinateY(paramInt2), this.focus, "", localAttributes);
  Object localObject = new Node(this);
  switch (this.focus.attributes.getShape())
  {
  default:
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
  }
  while (true)
  {
    ((Node)localObject).getNodeBounds();
    int j = Math.round(paramInt1 - ((Node)localObject).getNodeWidth() / 2.0F);
    int k = Math.round(paramInt2 - ((Node)localObject).getNodeHeight() / 2.0F);
    ((Node)localObject).setFloatLocation(this.commonResources.floatCoordinateX(j), this.commonResources.floatCoordinateY(k));
    ((Node)localObject).setIntLocation(j, k);
    this.backEnd.updateNodeLocation((Node)localObject);
    this.focus.addChild((Node)localObject);
    if (this.focus != null)
    {
      ((Node)localObject).line = new Line(this, (Node)localObject, this.commonResources);
      RelativeLayout.LayoutParams localLayoutParams2 = ((Node)localObject).line.getRelativeLayoutParams();
      this.relativeLayout.addView(((Node)localObject).line, localLayoutParams2);
      this.focus.bringToFront();
    }
    ((Node)localObject).setOnTouchListener(this.ui);
    Rect localRect = ((Node)localObject).getNodeBounds();
    RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.LayoutParams(localRect.right, localRect.bottom);
    localLayoutParams1.leftMargin = j;
    localLayoutParams1.topMargin = k;
    this.relativeLayout.addView((View)localObject, localLayoutParams1);
    /*this.adView.bringToFront();
    return localObject;
    localObject = new NoBorderNode(this, paramInt1, paramInt2, i, this.focus, this.commonResources, localAttributes);
    continue;
    localObject = new RectangleNode(this, paramInt1, paramInt2, i, this.focus, this.commonResources, localAttributes);
    continue;
    localObject = new RoundedRectangleNode(this, paramInt1, paramInt2, i, this.focus, this.commonResources, localAttributes);
    continue;
    localObject = new OvalNode(this, paramInt1, paramInt2, i, this.focus, this.commonResources, localAttributes);
    continue;*/
    localObject = new CircularNode(this, paramInt1, paramInt2, i, this.focus, this.commonResources, localAttributes);
  }
}
public void setNodeText(String paramString)
{
  dismissDialog(1);
  if (paramString.length() < 100)
    this.focus.setText(paramString);
  while (true)
  {
    this.backEnd.updateNodeText(this.focus);
    this.root.updateAllLayout();
    this.focus.setText(paramString.substring(0, 100));
    return;
  }
}
}
